import Vue from 'vue'
import Router from 'vue-router'
// 引入多个动态路由模块
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
Vue.use(Router)

import Layout from '@/layout'
// 动态路由表，项目中不同的用户可以访问不同的功能
export const asyncRoutes = [
  // 先空着这里，后面来补充功能
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter
]

// 固定不变的->'静态'路由规则数组
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'), // 路由懒加载
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard', // 路由重定向地址栏的地址变为dashboard(地址改变会再次重新匹配路由规则数组)
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
      // {
      // //   // info 路由配置
      //   path: 'info',
      //   name: 'Info',
      //   component: () => import('@/views/info/index'),
      //   meta: { title: 'Info', icon: 'dashboard' }
      // }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }, // 路由重定向配置404 地址栏显得好看
  {
    path: '/excel',
    component: Layout,
    hidden: true, // 不显示到左侧菜单
    children: [{
      path: '',
      component: () => import('@/views/excel')
    }]
  }
]

const createRouter = () =>
  new Router({
    // 路由发生改变时 网页滚到顶部
    scrollBehavior: () => ({ y: 0 }),
    // 静态和动态的路由组合到一起组成路由表
    routes: [...constantRoutes, ...asyncRoutes]
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
